2020-2021 Sunseeker Telemetry and Lighting System
mtif_ex1_pulseGeneration.c
Go to the documentation of this file.
1 /* --COPYRIGHT--,BSD
2  * Copyright (c) 2017, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  * --/COPYRIGHT--*/
32 //******************************************************************************
33 // MSP430FR60xx Demo - Pulse generation using the MTIF
34 //
35 // Description: Configure the MTIF for pulse generation. 8 pulses per 250ms.
36 // Configure the MTIF to also count the pulses - should get the same number
37 // generated.
38 //
39 // MSP430FR6047
40 // ---------------
41 // /|\| |
42 // | | |
43 // --|RST |
44 // | P1.0|---> LED
45 // | PJ.4|-LFXIN
46 // | PJ.5|-LFXOUT
47 // | MTIF_OUT_IN|<--> 8 pulses per 250ms
48 //
49 // Wallace Tran
50 // Texas Instruments Inc.
51 // June 2017
52 // Built with IAR Embedded Workbench V6.50 & Code Composer Studio V7.1
53 //******************************************************************************
54 #include "driverlib.h"
55 
56 volatile uint32_t pulseCount[10] = {0};
57 uint8_t i = 0;
58 
59 void main (void)
60 {
61  // Stop WDT
62  WDT_A_hold(WDT_A_BASE);
63 
64  // Configure P1.0 as output for LED
65  GPIO_setAsPeripheralModuleFunctionOutputPin(
66  GPIO_PORT_P1,
67  GPIO_PIN0,
68  GPIO_PRIMARY_MODULE_FUNCTION
69  );
70 
71  // Configure the PJ.4 & PJ.5 for LFXTIN/LFXTOUT
72  GPIO_setAsPeripheralModuleFunctionInputPin(
73  GPIO_PORT_PJ,
74  GPIO_PIN4 + GPIO_PIN5,
75  GPIO_PRIMARY_MODULE_FUNCTION
76  );
77 
78  // Disable the GPIO power-on default high-impedance mode to activate
79  // previously configured port settings
80  PMM_unlockLPM5();
81 
82  /* ---Begin initialize the clock system--- */
83  // Set DCO to 8MHz
84  CS_setDCOFreq(CS_DCORSEL_0, CS_DCOFSEL_3);
85  // Set ACLK = LFXTCLK, SMCLK = MCLK = DCO
86  CS_initClockSignal(CS_ACLK, CS_LFXTCLK_SELECT, CS_CLOCK_DIVIDER_1);
87  // MCLK = SMCLK = 1 MHz
88  CS_initClockSignal(CS_SMCLK, CS_DCOCLK_SELECT, CS_CLOCK_DIVIDER_8);
89  CS_initClockSignal(CS_MCLK, CS_DCOCLK_SELECT, CS_CLOCK_DIVIDER_8);
90  // Enable LFXT
91  CS_turnOnLFXT(CS_LFXT_DRIVE_3);
92  /* ---End initialize the clock system--- */
93 
94  /* ---Begin initialize the RTC--- */
95  // Stop RTC
96  RTC_C_holdClock(RTC_C_BASE);
97  // Set Prescalar for 1Hz interval
98  RTC_C_enableInterrupt(RTC_C_BASE, RTC_C_PRESCALE_TIMER1_INTERRUPT);
99  RTC_C_definePrescaleEvent(RTC_C_BASE, RTC_C_PRESCALE_1, RTC_C_PSEVENTDIVIDER_128);
100  // clear second and min counter
101  RTC_C_setCounterValue(RTC_C_BASE, 0);
102  // load PS counter
103  RTC_C_setPrescaleValue(RTC_C_BASE, RTC_C_PRESCALE_0, 0);
104  // clear the hold and start RTC
105  RTC_C_startClock(RTC_C_BASE);
106  /* ---End initialize the RTC--- */
107 
108  /* ---Begin initialize the MTIF--- */
109  // Enable the pulse generator, and set the initial grid frequency
110  // Pulse grid freq. = 1024Hz (frame freq. = 1024/256 = 4Hz)
111  MTIF_setPulseGenPulseGridFreq(MTIF_BASE, MTIF_PULSE_GRID_FREQUENCY_1024HZ);
112  MTIF_clearPulseGenCounter(MTIF_BASE);
113  MTIF_enablePulseGen(MTIF_BASE);
114  // KVAL = 4. 8 pulses per 256 periods of 1024Hz.
115  MTIF_setPulseGenCountNum(MTIF_BASE, MTIF_KVAL_BIT2);
116  // K-count update request
117  MTIF_clearPulseKCountUpdateRequest(MTIF_BASE);
118  // Wait for update acknowledge
119  while(MTIF_isPulseKCountUpdated(MTIF_BASE) == MTIF_K_COUNT_IS_NOT_UPDATED);
120  // Enable the MTIF terminal pins
121  MTIF_enableTestPortOutput(MTIF_BASE);
122  // Enable test port output
123  MTIF_enableTestPortTerminalActivationBySWAndHW(MTIF_BASE);
124  // Clear pulse counter and enable pulse counter
125  MTIF_clearPulseCounter(MTIF_BASE);
126  MTIF_enablePulseCounter(MTIF_BASE);
127  /* ---End initialize the MTIF--- */
128 
129  while(1)
130  {
131  // Enter LPM3 w/interrupt
132  __bis_SR_register(LPM3_bits | GIE);
133  // For debug
134  __no_operation();
135 
136  // Procedure to perform read of pulse count
137  // Poll PCRA to wait for completion of previous read
138  // Request pulse counter read
139  MTIF_setPulseCounterReadRequest(MTIF_BASE);
140  // Wait for stable read value
141  while(MTIF_isPulseCounterReadReady(MTIF_BASE) == MTIF_PULSE_COUNTER_NOT_READY_TO_READ);
142  // Read pulse counter value
143  pulseCount[i] = MTIF_getPulseCount(MTIF_BASE);
144  // Check if there was a 16-bit overflow
145  if(MTIF_getPulseCounterOverflow(MTIF_BASE) == MTIF_PULSE_COUNTER_OVERFLOW)
146  {
147  pulseCount[i] += 0x10000;
148  }
149  // Put breakpoint here to observe pulse count
150  __no_operation();
151  i++;
152  if(i == 10)
153  {
154  i = 0;
155  }
156  }
157 }
158 
159 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
160 #pragma vector=RTC_C_VECTOR
161 __interrupt void RTC_ISR(void)
162 #elif defined(__GNUC__)
163 void __attribute__ ((interrupt(RTC_C_VECTOR))) RTC_ISR (void)
164 #else
165 #error Compiler not supported!
166 #endif
167 {
168  switch(__even_in_range(RTCIV, RTCIV__RT1PSIFG))
169  {
170  case RTCIV__NONE: break; // No interrupts
171  case RTCIV__RTCOFIFG: break; // RTCOFIFG
172  case RTCIV__RTCRDYIFG: break; // RTCRDYIFG
173  case RTCIV__RTCTEVIFG: // RTCEVIFG
174 
175  break;
176  case RTCIV__RTCAIFG: break; // RTCAIFG
177  case RTCIV__RT0PSIFG: break; // RT0PSIFG
178  case RTCIV__RT1PSIFG:
179  GPIO_toggleOutputOnPin(GPIO_PORT_P1, GPIO_PIN0); // Toggle P1.0 LED
180  __bic_SR_register_on_exit(LPM3_bits | GIE); //Wake to perform read
181  __no_operation(); // For Debug
182  break; // RT1PSIFG
183  default: break;
184  }
185 }
void RTC_ISR(void)
__no_operation()
__bic_SR_register_on_exit(LPM3_bits|GIE)
volatile uint32_t pulseCount[10]
void main(void)
uint8_t i